/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.report.service;

import com.fzhucc.modules.report.domain.RaceReport;
import com.fzhucc.modules.system.service.DictDetailService;
import com.fzhucc.modules.system.service.DictService;
import com.fzhucc.modules.system.service.dto.DictDetailDto;
import com.fzhucc.modules.system.service.dto.DictDto;
import com.fzhucc.modules.system.service.dto.DictQueryCriteria;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.report.repository.RaceReportRepository;
import com.fzhucc.modules.report.domain.dto.RaceReportDto;
import com.fzhucc.modules.report.domain.dto.RaceReportQueryCriteria;
import com.fzhucc.modules.report.service.mapstruct.RaceReportMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2023-08-16
**/
@Service
@RequiredArgsConstructor
public class RaceReportService {

    private final RaceReportRepository raceReportRepository;
    private final RaceReportMapper raceReportMapper;

    private final DictService dictService;

    private final DictDetailService dictDetailService;

    public Map<String,Object> queryAll(RaceReportQueryCriteria criteria, Pageable pageable){
        Page<RaceReport> page = raceReportRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(raceReportMapper::toDto));
    }

    public List<RaceReportDto> queryAll(RaceReportQueryCriteria criteria){
        return raceReportMapper.toDto(raceReportRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public RaceReportDto findById(Integer id) {
        RaceReport raceReport = raceReportRepository.findById(id).orElseGet(RaceReport::new);
        ValidationUtil.isNull(raceReport.getId(),"RaceReport","id",id);
        return raceReportMapper.toDto(raceReport);
    }

    @Transactional(rollbackFor = Exception.class)
    public RaceReportDto create(RaceReport resources) {
        return raceReportMapper.toDto(raceReportRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(RaceReport resources) {
        RaceReport raceReport = raceReportRepository.findById(resources.getId()).orElseGet(RaceReport::new);
        ValidationUtil.isNull( raceReport.getId(),"RaceReport","id",resources.getId());
        raceReport.copy(resources);
        raceReportRepository.save(raceReport);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            raceReportRepository.deleteById(id);
        }
    }

    public void download(List<RaceReportDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RaceReportDto raceReport : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("封面", raceReport.getPic());
            map.put("标题", raceReport.getTitle());
            map.put("发布时间", raceReport.getReleaseTime());
            map.put("来源 字典race_source", raceReport.getSource());
            map.put("内容  ", raceReport.getContext());
            map.put("内容分类  字典 race_context_type", raceReport.getContextType());
            map.put("排序", raceReport.getSort());
            map.put("是否显示", raceReport.getIsShow());
            map.put("年份", raceReport.getYear());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public List<DictDetailDto> getYearType() {
        DictQueryCriteria dictQueryCriteria = new DictQueryCriteria();
        dictQueryCriteria.setBlurry("race_year_type");
        List<DictDto> list = dictService.queryAll(dictQueryCriteria);
        if (list != null && list.size() == 1) {
            List<DictDetailDto> list1 = dictDetailService.getDictByDictIdByDictSort(list.get(0).getId());
            return list1;
        }
        return null;
    }
}